import { DrawerForm, ProFormDigit, ProFormSwitch, ProFormText } from '@ant-design/pro-components';
import { Button, Form } from 'antd';
import { useState } from 'react';
import api from './request';

export type FormConfig = { Component: any; props: Record<string, any> };
export interface Props {
  data?: PointsTransferApi.ListResponse;
  onSuccess?: () => void;
  onCancel?: () => void;
}
function PointsTransfterForm(props: Props) {
  const { data } = props;

  const [form] = Form.useForm();
  const [formData, setFormData] = useState<any>({});
  const [, setInfo] = useState<Partial<PointsTransferApi.InfoResponse>>({});

  const getInfo = async (params: PointsTransferApi.InfoParams) => {
    const info = await api.getInfo(params);
    setInfo(info);
    form.setFieldsValue(info);
    setFormData(info);
  };

  const onOpenChange = (open: boolean) => {
    if (open) {
      if (data && data.id) {
        getInfo({ id: data.id });
      }
    } else {
      setInfo({});
      form.resetFields();
      setFormData({});
      props.onCancel?.();
    }
  };

  return (
    <DrawerForm
      readonly
      autoFocusFirstInput
      title={`优惠券`}
      form={form}
      drawerProps={{ destroyOnClose: true }}
      resize={{
        maxWidth: window.innerWidth * 0.8,
        minWidth: 400,
        onResize() {},
      }}
      trigger={<Button type="link">详情</Button>}
      onOpenChange={onOpenChange}
    >
      <ProFormText
        required
        name="name"
        label="优惠券名称"
        placeholder="请输入优惠券名称"
        rules={[{ required: true }]}
        fieldProps={{
          value: formData.name,
          onChange: (e) => setFormData({ ...formData, name: e.target.value }),
        }}
      />
      <ProFormText
        required
        name="mobileno"
        label="手机号"
        placeholder="请输入手机号"
        rules={[{ required: true }, { pattern: /^1\d{10}/, message: '请输入正确的手机号' }]}
        fieldProps={{
          value: formData.mobileno,
          onChange: (e) => setFormData({ ...formData, mobileno: e.target.value }),
        }}
      />
      <ProFormText
        required
        name="licensePlateNumber"
        label="车牌号"
        placeholder="请输入车牌号"
        rules={[{ required: true }]}
        fieldProps={{
          value: formData.licensePlateNumber,
          onChange: (e) => setFormData({ ...formData, licensePlateNumber: e.target.value }),
        }}
      />
      <ProFormText
        required
        name="vin"
        label="车架号"
        placeholder="请输入车架号"
        rules={[{ required: true }]}
        fieldProps={{
          value: formData.vin,
          onChange: (e) => setFormData({ ...formData, vin: e.target.value }),
        }}
      />
      <ProFormDigit
        required
        name="integral"
        min={1}
        label="积分"
        placeholder="请输入积分"
        rules={[{ required: true }]}
        fieldProps={{
          value: formData.integral,
          onChange: (value) => setFormData({ ...formData, integral: value }),
        }}
      />

      <ProFormSwitch
        required
        name="status"
        label="状态"
        placeholder="请选择状态"
        checkedChildren="启用"
        unCheckedChildren="禁用"
        rules={[{ required: true }]}
        fieldProps={{
          checked: !!formData.status,
          onChange: (value) => {
            setFormData({ ...formData, status: value ? 1 : 0 });
          },
        }}
      />
    </DrawerForm>
  );
}

export default PointsTransfterForm;
